package com.blockmeta.bbs.businesslibrary.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import androidx.annotation.ColorInt;
import androidx.appcompat.widget.AppCompatTextView;
import android.util.AttributeSet;

import com.blockmeta.bbs.businesslibrary.R;

/**
 * @author AirThor
 * @date 2019/9/29.
 */
public class RoundCornerTextView extends AppCompatTextView {
    private float topLeftRadius;
    private float topRightRadius;
    private float bottomLeftRadius;
    private float bottomRightRadius;
    public RoundCornerTextView(Context context) {
        this(context,null);
    }

    public RoundCornerTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundCornerTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerTextView);
            float radius = ta.getDimension(R.styleable.RoundCornerTextView_textRadius,0);
            topLeftRadius = ta.getDimension(R.styleable.RoundCornerTextView_textTopLeftRadius, radius);
            topRightRadius = ta.getDimension(R.styleable.RoundCornerTextView_textTopRightRadius, radius);
            bottomLeftRadius = ta.getDimension(R.styleable.RoundCornerTextView_textBottomLeftRadius, radius);
            bottomRightRadius = ta.getDimension(R.styleable.RoundCornerTextView_textBottomRightRadius, radius);
            float strokeWidth = ta.getDimension(R.styleable.RoundCornerTextView_textStrokeWidth,0);
            int strokeColor = ta.getColor(R.styleable.RoundCornerTextView_textStrokeColor,Color.WHITE);
            int backGroundColor = ta.getColor(R.styleable.RoundCornerTextView_textBackGroundColor,Color.WHITE);
            ta.recycle();

            GradientDrawable backGround = new GradientDrawable();
            backGround.setColor(backGroundColor);
            if (topLeftRadius > 0 || topRightRadius > 0 || bottomLeftRadius > 0 || bottomRightRadius > 0) {
                float[] radii = new float[]{
                        topLeftRadius, topLeftRadius,
                        topRightRadius, topRightRadius,
                        bottomRightRadius, bottomRightRadius,
                        bottomLeftRadius, bottomLeftRadius
                };
                backGround.setCornerRadii(radii);
            }
            if (strokeWidth > 0){
                backGround.setStroke((int) strokeWidth,strokeColor);
            }
            setBackground(backGround);
        }
    }

    private GradientDrawable getBackGroundDrawable(){
        return (GradientDrawable) getBackground();
    }

    public void setBackGroundColor(@ColorInt int backGroundColor) {
        GradientDrawable gradientDrawable = getBackGroundDrawable();
        gradientDrawable.setColor(backGroundColor);
    }

}
